package com.eralink.unionuhfreader.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.eralink.unionuhfreader.R;
import com.eralink.unionuhfreader.bean.FinderReportReaderBean;

import java.util.List;

public class FinderReportReaderListViewAdapter extends BaseAdapter {
    private final List<FinderReportReaderBean> finderReportReaderBeanList;
    private final LayoutInflater layoutInflater;

    private int selectedItem = -1;

    public FinderReportReaderListViewAdapter(Context context, List<FinderReportReaderBean> finderReportReaderBeanList) {
        this.layoutInflater = LayoutInflater.from(context);
        this.finderReportReaderBeanList = finderReportReaderBeanList;
    }

    public void setSelectedItem(int selectedItem) {
        this.selectedItem = selectedItem;
    }

    @Override
    public int getCount() {
        return finderReportReaderBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return finderReportReaderBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        TagListView tagListView;
        if (view == null){
            view = layoutInflater .inflate(R.layout.finder_report_reader_item,parent,false);
            tagListView = new TagListView();
            tagListView.tvIndex = view.findViewById(R.id.tv_number);
            tagListView.tvName = view.findViewById(R.id.tv_name);
            tagListView.tvVersion = view.findViewById(R.id.tv_version);
            tagListView.tvIP = view.findViewById(R.id.tv_ip);
            tagListView.tvMac = view.findViewById(R.id.tv_mac);
            tagListView.tvNetMode = view.findViewById(R.id.tv_net_mode);
            view.setTag(tagListView);
        }
        else {
            tagListView = (TagListView) view.getTag();
        }

        tagListView.tvIndex.setText(String.valueOf(finderReportReaderBeanList.get(position).getIndex() + 1));
        tagListView.tvName.setText(finderReportReaderBeanList.get(position).getName());
        tagListView.tvVersion.setText(finderReportReaderBeanList.get(position).getVersion());
        tagListView.tvIP.setText(finderReportReaderBeanList.get(position).getIp());
        tagListView.tvMac.setText(finderReportReaderBeanList.get(position).getMac());
        tagListView.tvNetMode.setText(finderReportReaderBeanList.get(position).getNetMode() == 1 ? "Tcp Server" : "Tcp Client");

        if (selectedItem == position) {
            tagListView.tvIndex.setTextColor(Color.BLUE);
            tagListView.tvName.setTextColor(Color.BLUE);
            tagListView.tvVersion.setTextColor(Color.BLUE);
            tagListView.tvIP.setTextColor(Color.BLUE);
            tagListView.tvMac.setTextColor(Color.BLUE);
            tagListView.tvNetMode.setTextColor(Color.BLUE);
        } else {
            tagListView.tvIndex.setTextColor(Color.BLACK);
            tagListView.tvName.setTextColor(Color.BLACK);
            tagListView.tvVersion.setTextColor(Color.BLACK);
            tagListView.tvIP.setTextColor(Color.BLACK);
            tagListView.tvMac.setTextColor(Color.BLACK);
            tagListView.tvNetMode.setTextColor(Color.BLACK);
        }

        return view;
    }

    static class TagListView {
        public TextView tvIndex;
        public TextView tvName;
        public TextView tvVersion;
        public TextView tvIP;
        public TextView tvMac;
        public TextView tvNetMode;
    }
}
